package net.ib.mtalk.network.pipeline;

import net.ib.mtalk.util.MTalkLogWriter;

import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.frame.LengthFieldPrepender;

import android.os.PowerManager;


/**
 * ChannelBuffer -> Stream <br/>
 * @author urunimi
 *
 */
public class BufferStreamEncoder extends LengthFieldPrepender {
	
	private PowerManager.WakeLock mWakeLock; 

	public BufferStreamEncoder(PowerManager.WakeLock wakeLock) {
		super(4);	// 4 byte big-endian int 는 자바 언어 스펙이다.
		this.mWakeLock = wakeLock;
	}
	
	@Override
	protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
		MTalkLogWriter.v(this.getClass().getSimpleName()+".encode() - STT");

		if(mWakeLock != null) mWakeLock.acquire();
		try {
			return super.encode(ctx, channel, msg);
		} finally {
			if(mWakeLock != null) mWakeLock.release();

			MTalkLogWriter.v(this.getClass().getSimpleName()+".encode() - END");
		}
	}
}
